/**
 *博尔信基础开发平台
 *<br> 版权声明：厦门博尔信软件有限公司版权所有,违者必究
 *<br> Copyright:  Copyright (c) 2012
 *<br> Company:pearlsoft
 *<br> @author 蒋勇华
 *<br> 2012-5-7
 *<br> @version 1.0
 *————————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *——————————————————————————————————————
 */
package com.pearlsoft.webapp.sysmgr.role.service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Service;

import com.pearlsoft.commons.utils.UUIDUtil;
import com.pearlsoft.webapp.core.WebException;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.Result;
import com.pearlsoft.webapp.core.service.BaseSimpleJdbcService;

/**
 *描述： 
 *
 * 
 */
@Service
public class RoleService extends BaseSimpleJdbcService{
	
	/***
	 * 检查是否有重复的角色名称
	 * @param roleId
	 * @param roleName
	 * @return
	 */
	public boolean checkNameDup(String roleId,String roleName){
		String sql ="";
		Map<String,String> params = new HashMap<String,String>();
		if( StringUtils.isBlank(roleId)){
			sql =" select count(*) from sys_role where role_name = :role_name";
			params.put("role_name", roleName);
		}else{
			sql =" select count(*) from sys_role where role_name = :role_name and role_id <> :role_id";
			params.put("role_name", roleName);
			params.put("role_id", roleId);
		}
		int count = this.getBaseSimpleJdbcDao().queryForInt(sql, params);
		return count > 0 ? true:false;
	}
	
	/***
	 * 通过角色ID得到角色
	 * @param roleId
	 * @return
	 */
	public Record getById(String roleId){
		String sql = "SELECT ROLE_ID, ROLE_NAME, C_STATUS, C_REMARK FROM SYS_ROLE WHERE ROLE_ID=:ROLE_ID";
		Map<String,String> param = new HashMap<String,String>();
		param.put("ROLE_ID", roleId);
		Record record = this.getBaseSimpleJdbcDao().queryOne(sql,param,RoleRowMapperHelper.generalRowMapper());
		return record;
	}
	
	public void deleteById(String roleId) {
		String sql1 = "delete from SYS_ROLE where ROLE_ID=:ROLE_ID";
		String sql2 ="delete from SYS_ROLE_MENU where ROLE_ID=:ROLE_ID";
		String sql3 ="delete from SYS_USER_ROLE where ROLE_ID=:ROLE_ID";
		Map<String,String> param = new HashMap<String,String>();
		param.put("ROLE_ID", roleId);
		
		this.getBaseSimpleJdbcDao().update(sql1, param);
		this.getBaseSimpleJdbcDao().update(sql2, param);
		this.getBaseSimpleJdbcDao().update(sql3, param);

	}
	
	public void save(Record record) throws WebException{
		String roleId =(String)record.getValue("ROLE_ID");
		String roleName = (String)record.getValue("ROLE_NAME");
		boolean isNameDup = this.checkNameDup(roleId, roleName);
		if( isNameDup ){
			throw new WebException("角色名称已经存在");
		}
		if(StringUtils.isBlank(roleId)){
			this.insertRecord(record);
		}else{
			this.updateRecord(record);
		}
	}	

	private void insertRecord( Record record)  {
		String sql = "insert into SYS_ROLE(ROLE_ID,ROLE_NAME,C_STATUS,C_REMARK) " +
				"values(:ROLE_ID,:ROLE_NAME,:C_STATUS,:C_REMARK)";
		String id = UUIDUtil.get();
		record.setValue("ROLE_ID", id);
		Map<String, String> param = new HashMap<String, String>();
		param.put("ROLE_ID", (String) record.getValue("ROLE_ID"));
		param.put("ROLE_NAME", (String) record.getValue("ROLE_NAME"));
		param.put("C_STATUS", (String) record.getValue("C_STATUS"));
		param.put("C_REMARK", (String) record.getValue("C_REMARK"));
		this.getBaseSimpleJdbcDao().update(sql, param);
		
	}
	
	private void updateRecord( Record record)   {
		String sql = "update SYS_ROLE set ROLE_NAME=:ROLE_NAME,C_STATUS=:C_STATUS,C_REMARK=:C_REMARK where ROLE_ID=:ROLE_ID";
		Map<String, String> param = new HashMap<String, String>();
		param.put("ROLE_ID", (String) record.getValue("ROLE_ID"));
		param.put("ROLE_NAME", (String) record.getValue("ROLE_NAME"));
		param.put("C_STATUS", (String) record.getValue("C_STATUS"));
		param.put("C_REMARK", (String) record.getValue("C_REMARK"));
		this.getBaseSimpleJdbcDao().update(sql, param);
	}
	
	public void saveRoleMenu(String roleId,String menuIds){
		String delsql ="delete from SYS_ROLE_MENU where ROLE_ID=:ROLE_ID";
		Map<String, String> param1 = new HashMap<String, String>();
		param1.put("ROLE_ID", roleId);
		this.getBaseSimpleJdbcDao().update(delsql, param1);
		
		String inertSql = "insert into sys_role_menu(ROLE_MENU_ID,ROLE_ID,MENU_ID) " +
				" values(:ROLE_MENU_ID,:ROLE_ID,:MENU_ID)";
		String[] arrId= menuIds.split(",");
		for(final String menuId:arrId ){
			if( !StringUtils.isBlank(menuId)){
				Map<String, String> param2 = new HashMap<String, String>();
				param2.put("ROLE_MENU_ID", UUIDUtil.get());		
				param2.put("ROLE_ID", roleId);		
				param2.put("MENU_ID", menuId);	
				this.getBaseSimpleJdbcDao().update(inertSql, param2);
			}
		}
	}
}
